// packageE/coupons/coupons.js

var app = getApp();
const DrainageScene = require('../../../utils/drainage_scene.js')

Component({
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
    page_id: {
      type: null,
    },
    bottomShowBol: {
      type: null,
    },
    bottomShowBolIndex: {
      type: null,
    },
    index: {
      type: null,
    },
  },
  // 私有数据，可用于模板渲染
  data: {
    emptyImage:  `${app.globalData.imgPath}image.png`,
    clientWidth: "375",
    coupons_list: [],

    isLoadMore: true, //true 可以加载更多
    page: 1,
    total_page: 0,
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {},
    moved() {},
    detached() {},
  },
  

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {},
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    this.setData({
      clientWidth: wx.getSystemInfoSync().windowWidth,
    });

    if (this.data.datas.list.data) {
      this.setData({
        total_page: this.data.datas.list.last_page,
      });
      if (this.data.total_page <= this.data.page) {
        this.setData({
          isLoadMore: false,
        });
      }
      if (this.data.datas.list.data.length > this.data.datas.get_number) {
        this.data.datas.list.data.splice(
          this.data.datas.get_number,
          this.data.datas.list.data.length - this.data.datas.get_number
        );
        this.setData({
          isLoadMore: false,
        });
      }
      this.setData({
        coupons_list: this.data.datas.list.data,
      });
    } else {
      if (this.data.datas.list.length <= 0) {
        this.setData({
          isLoadMore: false,
        });
      }
      this.setData({
        coupons_list: this.data.datas.list,
      });
    }
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
    selectedcoupon(evt) {
      let isBindDrainageScene = DrainageScene.bindDrainageScene({
        scene: 3,
        title: '领取优惠券'
      });
      if(!isBindDrainageScene) return;//爆客码绑定场景

      let item = evt.currentTarget.dataset.item;
      let index = evt.currentTarget.dataset.index;
      if (item.api_availability == 3) {
        return;
      }
      let urlStr = app.getNetAddresss("coupon.member-coupon.get-coupon");
      app._postNetWork({
        url: urlStr,
        data: {
          coupon_id: item.id,
        },
        success: (resdata) => {
          var res = resdata.data;
          if (res.result != 1) {
            if(res.data.reason){
              wx.showModal({
                title:res.msg,
                content:res.data.reason,
                showCancel:false
              });
            }else{
              wx.showToast({
                icon: 'none',
                title: res.msg,
                duration: 1000
              });
            }
            return;
          }
          this.data.coupons_list[index] = res.data;
          this.setData({
            coupons_list: this.data.coupons_list,
          });
          wx.showModal({
            title: "提示",
            content: "已领取，可到个人中心--优惠券查看",
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: "/packageA/member/coupon_v2/coupon_v2",
                });
              } else if (res.cancel) {
                // console.log('用户点击取消')
              }
            },
          });
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    },
    // 获取更多数据
    getMoreData() {
      let that = this;
      that.setData({
        isLoadMore: false,
      });
      if (
        that.data.page >= that.data.total_page ||
        this.data.coupons_list.length > this.data.datas.get_number
      ) {
        return;
      } else {
        that.setData({
          page: that.data.page + 1,
        });
        let urlStr = app.getNetAddresss("home-page.get-decorate-page");
        urlStr += "&page=" + that.data.page;
        urlStr += "&decorate_id=" + that.data.page_id;
        urlStr += "&component_id=" + that.data.component_id;
        urlStr += "&component_key=U_coupons";
        app._getNetWork({
          url: urlStr,
          success: (resdata) => {
            var res = resdata.data;
            if (res.result == 1) {
              if (that.data.page < that.data.total_page) {
                that.setData({
                  isLoadMore: true,
                });
              }

              let data = that.data.coupons_list.concat(res.data.data);

              if (data.length > that.data.datas.get_number) {
                data.splice(
                  that.data.datas.get_number,
                  data.length - that.data.datas.get_number
                );
                that.setData({
                  isLoadMore: false,
                });
              }
              that.setData({
                coupons_list: data,
              });
            } else {
              that.setData({
                page: that.data.page - 1,
                isLoadMore: false,
              });
            }
          },
          fail: function (res) {
            console.log(res);
          },
        });
      }
    },
  },
});
